<%@page import="com.google.appengine.api.datastore.Query.SortDirection"%>
<%@page import="hibari.client.model.Week"%>
<%@page import="com.google.appengine.api.datastore.Query.FilterOperator"%>
<%@page import="hibari.client.model.Music"%>
<%@page import="javax.jdo.PersistenceManager"%>
<%@page import="com.google.appengine.api.datastore.Entity"%>
<%@page import="java.util.Iterator"%>
<%@page import="com.google.appengine.api.datastore.PreparedQuery"%>
<%@page
	import="com.google.appengine.api.datastore.DatastoreServiceFactory"%>
<%@page import="com.google.appengine.api.datastore.DatastoreService"%>
<%@page import="com.google.appengine.api.datastore.Query"%>
<%@page import="hibari.PMF"%>
<%@page import="hibari.server.lib.AdminLog"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%
    String role = (String) session.getAttribute("role");
    if (role == null) {
        response.sendRedirect("login.jsp");
    }

    PersistenceManager pm = PMF.get().getPersistenceManager();

    Query query = new Query("Week");
    query.addFilter("alive", FilterOperator.EQUAL, true);
    query.addSort("weekNumber", SortDirection.DESCENDING);

    DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
    PreparedQuery pq = ds.prepare(query);
    Iterator<Entity> iter = pq.asIterator();
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hibari Dashboard</title>
<script type="text/javascript" src="jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="common.js"></script>
<link rel="stylesheet" type="text/css" href="styles.css" />
</head>
<body onload="main()">
	<%=AdminLog.get(session).render(session)%>
	<a href="/Admin/index.jsp">return to console</a>
	<fieldset>
		<legend>Musics</legend>
		<table>
			<tr>
				<th>ID</th>
				<th>Week No.</th>
				<th>Title</th>
				<th colspan="2">Tool</th>
			</tr>

			<%
			    while (iter.hasNext()) {
			        Entity entity = iter.next();
			        Week week = pm.getObjectById(Week.class, entity.getKey());
			%>
			<tr>
				<td><%=week.getKey()%></td>
				<td><%=week.getWeekNumber()%></td>
				<td><%=week.getDate()%></td>
				<td><a href="EditWeek.jsp?id=<%=week.getKey()%>">Edit</a>
				
				<a target="_blank" href="/#week<%=week.getWeekNumber()%>">View</a>
				</td>
				<td>
					<form action="/edit" enctype="multipart/form-data" method="post">
						<input type="hidden" name="callback" value="/Admin/WeekList.jsp" />
						<input type="hidden" name="operation" value="removeWeek">
						<input type="hidden" name="id" value="<%=week.getKey()%>" /> <input
							type="submit" value="Delete" class="delete-button" />
					</form></td>
			</tr>

			<%
			    }
			%>
		</table>
	</fieldset>


</body>
</html>